package feizhou.untils.tools;

import java.util.Calendar;
import java.util.Date;

/**
 * 
 * @ClassName : TimeAndDateUtil.java
 * @Description: 时间及日期操作辅助类
 * @Modification history 2013-8-5 下午2:45:08
 * @Autor : driver.deng driver_deng@163.com
 * @Date : 2013-8-5 下午2:45:08
 * @version :
 * 
 */
public class TimeUtil
{
    public static long INVALID_TIME = DateUtil.parse("1990-01-01 00:00:00").getTime();

	public static void main(String[] args) {
		System.out.println(getStartDateOfCurWeek());
		System.out.println(getEndDateOfCurWeek());
		System.out.println(getStartDateOfNextWeek());
		System.out.println(getEndDateOfNextWeek());

		System.out.println(getStartDateOfCurMonth());
		System.out.println(getEndDateOfCurMonth());
		System.out.println(getStartDateOfNextMonth());
		System.out.println(getStartDateOfBeforeYear());
		System.out.println(getStartDateOfBeforeSixMonth());
		System.out.println(getStartDateOfBeforeThreeMonth());
		System.out.println(getStartDateOfBeforeOneMonth());
		System.out.println(getStartDateOfBeforeOneWeek());
		
		System.out.println(getCurYMDStr());
		System.out.println(getStartDateOfBeforeXHour(System.currentTimeMillis(), 8));
		System.out.println(getDateOfBeforeXDay(30));
		
		System.out.println(getStartDateOfCurDay());
	}

	/** 本周开始时间 -国内标准 -周一 */
	public static Date getStartDateOfCurWeek() {
		Calendar calendar = Calendar.getInstance();
		int curDayOfWeek = calendar.get(Calendar.DAY_OF_WEEK);
		if (curDayOfWeek == Calendar.SUNDAY) {
			int curWeek = calendar.get(Calendar.WEEK_OF_MONTH);
			calendar.set(Calendar.WEEK_OF_MONTH, curWeek - 1);
		}
		calendar.set(Calendar.DAY_OF_WEEK, Calendar.MONDAY);
		calendar.set(Calendar.HOUR_OF_DAY, 0);
		calendar.set(Calendar.MINUTE, 0);
		calendar.set(Calendar.SECOND, 0);
		calendar.set(Calendar.MILLISECOND, 0);
		return calendar.getTime();
	}
	
	/** 今天的开始时间*/
	public static Date getStartDateOfCurDay() {
		Calendar calendar = Calendar.getInstance();
		calendar.set(Calendar.HOUR_OF_DAY, 0);
		calendar.set(Calendar.MINUTE, 0);
		calendar.set(Calendar.SECOND, 0);
		calendar.set(Calendar.MILLISECOND, 0);
		return calendar.getTime();
	}

	/** 本周结束时间 -国内标准 周日 */
	public static Date getEndDateOfCurWeek() {
		Calendar calendar = Calendar.getInstance();
		int curDayOfWeek = calendar.get(Calendar.DAY_OF_WEEK);
		if (curDayOfWeek == Calendar.SUNDAY) {
			int curWeek = calendar.get(Calendar.WEEK_OF_MONTH);
			calendar.set(Calendar.WEEK_OF_MONTH, curWeek - 1);
		}
		int curWeek = calendar.get(Calendar.WEEK_OF_MONTH);
		calendar.set(Calendar.WEEK_OF_MONTH, curWeek + 1);
		calendar.set(Calendar.DAY_OF_WEEK, Calendar.SUNDAY);
		calendar.set(Calendar.HOUR_OF_DAY, 23);
		calendar.set(Calendar.MINUTE, 59);
		calendar.set(Calendar.SECOND, 59);
		calendar.set(Calendar.MILLISECOND, 999);
		return calendar.getTime();
	}

	/** 下周开始时间 -国内标准 周一 */
	public static Date getStartDateOfNextWeek() {
		Calendar calendar = Calendar.getInstance();
		int curDayOfWeek = calendar.get(Calendar.DAY_OF_WEEK);
		if (curDayOfWeek == Calendar.SUNDAY) {
			int curWeek = calendar.get(Calendar.WEEK_OF_MONTH);
			calendar.set(Calendar.WEEK_OF_MONTH, curWeek - 1);
		}
		int curWeek = calendar.get(Calendar.WEEK_OF_MONTH);
		calendar.set(Calendar.WEEK_OF_MONTH, curWeek + 1);
		calendar.set(Calendar.DAY_OF_WEEK, Calendar.MONDAY);
		calendar.set(Calendar.HOUR_OF_DAY, 0);
		calendar.set(Calendar.MINUTE, 0);
		calendar.set(Calendar.SECOND, 0);
		calendar.set(Calendar.MILLISECOND, 0);
		return calendar.getTime();
	}

	/** 下周结束时间 -国内标准 周日 */
	public static Date getEndDateOfNextWeek() {
		Calendar calendar = Calendar.getInstance();
		int curDayOfWeek = calendar.get(Calendar.DAY_OF_WEEK);
		if (curDayOfWeek == Calendar.SUNDAY) {
			int curWeek = calendar.get(Calendar.WEEK_OF_MONTH);
			calendar.set(Calendar.WEEK_OF_MONTH, curWeek - 1);
		}
		int curWeek = calendar.get(Calendar.WEEK_OF_MONTH);
		calendar.set(Calendar.WEEK_OF_MONTH, curWeek + 2);
		calendar.set(Calendar.DAY_OF_WEEK, Calendar.SUNDAY);
		calendar.set(Calendar.HOUR_OF_DAY, 23);
		calendar.set(Calendar.MINUTE, 59);
		calendar.set(Calendar.SECOND, 59);
		calendar.set(Calendar.MILLISECOND, 999);
		return calendar.getTime();
	}

	/** 本月开始时间 */
	public static Date getStartDateOfCurMonth() {
		Calendar calendar = Calendar.getInstance();
		calendar.setTime(new Date());
		calendar.set(Calendar.DAY_OF_MONTH, 1);
		calendar.set(Calendar.HOUR_OF_DAY, 0);
		calendar.set(Calendar.MINUTE, 0);
		calendar.set(Calendar.SECOND, 0);
		calendar.set(Calendar.MILLISECOND, 0);
		return calendar.getTime();
	}

	/** 本月结束时间 */
	public static Date getEndDateOfCurMonth() {
		Calendar calendar = Calendar.getInstance();
		int month = calendar.get(Calendar.MONTH);
		calendar.set(Calendar.MONTH, month + 1);
		calendar.set(Calendar.DAY_OF_MONTH, 0);
		calendar.set(Calendar.HOUR_OF_DAY, 23);
		calendar.set(Calendar.MINUTE, 59);
		calendar.set(Calendar.SECOND, 59);
		calendar.set(Calendar.MILLISECOND, 999);
		return calendar.getTime();

	}

	/** 得到下一月份的月开始时间 */
	public static Date getStartDateOfNextMonth() {
		Calendar calendar = Calendar.getInstance();
		int month = calendar.get(Calendar.MONTH);
		calendar.set(Calendar.MONTH, month + 1);
		calendar.set(Calendar.DAY_OF_MONTH, 1);
		calendar.set(Calendar.HOUR_OF_DAY, 0);
		calendar.set(Calendar.MINUTE, 0);
		calendar.set(Calendar.SECOND, 0);
		calendar.set(Calendar.MILLISECOND, 0);
		return calendar.getTime();
	}
	
	/** 得到一年前的时间 */
	public static Date getStartDateOfBeforeYear() {
		Calendar calendar = Calendar.getInstance();
		int year = calendar.get(Calendar.YEAR);
		calendar.set(Calendar.YEAR, (year-1));
		return calendar.getTime();
	}

	/** 得到六个月前的时间 */
	public static Date getStartDateOfBeforeSixMonth() {
		Calendar calendar = Calendar.getInstance();
		int month = calendar.get(Calendar.MONTH);
		calendar.set(Calendar.MONTH, month -6);
		return calendar.getTime();
	}
	
	/** 得到三个月前的时间 */
	public static Date getStartDateOfBeforeThreeMonth() {
		Calendar calendar = Calendar.getInstance();
		int month = calendar.get(Calendar.MONTH);
		calendar.set(Calendar.MONTH, month -3);
		return calendar.getTime();
	}
	
	/** 得到一个月前的时间 */
	public static Date getStartDateOfBeforeOneMonth() {
		Calendar calendar = Calendar.getInstance();
		int month = calendar.get(Calendar.MONTH);
		calendar.set(Calendar.MONTH, month -1);
		return calendar.getTime();
	}
	
	/** 得到一周前的时间 */
	public static Date getStartDateOfBeforeOneWeek() {
		Calendar calendar = Calendar.getInstance();
		int week = calendar.get(Calendar.WEEK_OF_MONTH);
		calendar.set(Calendar.WEEK_OF_MONTH, week -1);
		return calendar.getTime();
	}
	
	/**得到当前时间的年月日组成的字符串,格式如下2014_01_01*/
	public static String getCurYMDStr(){
		String restr="";
		Calendar calendar = Calendar.getInstance();
		int year=calendar.get(Calendar.YEAR);
		String mStr="";
		int month=calendar.get(Calendar.MONTH)+1;
		if(month<10)mStr="0"+month;else mStr=""+month;
		String dayStr="";
		int day =calendar.get(Calendar.DAY_OF_MONTH);
		if(day<10)dayStr="0"+day;else dayStr=""+day;
		restr=year+"_"+mStr+"_"+dayStr;
		return restr;
	}
	
	/** 得到指定时间后X小时的时间*/
	public static Date getStartDateOfAfterXHour(long beforeTime,int X) {
		Calendar calendar = Calendar.getInstance();
		calendar.setTimeInMillis(beforeTime);
		int hour = calendar.get(Calendar.HOUR_OF_DAY);
		calendar.set(Calendar.HOUR_OF_DAY, hour +X);
		return calendar.getTime();
	}
	
	/** 得到指定时间前X小时的时间*/
	public static Date getStartDateOfBeforeXHour(long beforeTime,int X) {
		Calendar calendar = Calendar.getInstance();
		calendar.setTimeInMillis(beforeTime);
		int hour = calendar.get(Calendar.HOUR_OF_DAY);
		calendar.set(Calendar.HOUR_OF_DAY, hour -X);
		return calendar.getTime();
	}
	
	/** 得到X天后的起始时间*/
	public static Date getStartDateOfNextXDay(int x) {
		Calendar calendar = Calendar.getInstance();
		int day=calendar.get(Calendar.DAY_OF_MONTH);
		calendar.set(Calendar.DAY_OF_MONTH, day+x);
		calendar.set(Calendar.HOUR_OF_DAY,0);
		calendar.set(Calendar.MINUTE,0);
		calendar.set(Calendar.SECOND,0);
		calendar.set(Calendar.MILLISECOND,0);
		return calendar.getTime();
	}
	
	/** 得到X天前的时间*/
	public static Date getDateOfBeforeXDay(int x) {
		Calendar calendar = Calendar.getInstance();
		int day=calendar.get(Calendar.DAY_OF_MONTH);
		calendar.set(Calendar.DAY_OF_MONTH, day-x);
		return calendar.getTime();
	}
}
